package org.jeecg.modules.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.manager.entity.Car;
import org.jeecg.modules.manager.entity.DeletedEnum;
import org.jeecg.modules.manager.entity.GoodsType;
import org.jeecg.modules.manager.mapper.CarMapper;
import org.jeecg.modules.manager.mapper.GoodsTypeMapper;
import org.jeecg.modules.manager.service.IGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Optional;

/**
 * @Description: 商品类型表
 * @Author: jeecg-boot
 * @Date:   2021-11-19
 * @Version: V1.0
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {
    @Autowired
    CarMapper carMapper;

    @Autowired
    GoodsTypeMapper goodsTypeMapper;

    @Override
    public int checkRemoveById(String goodsTypeId) {
        List<Car> cars = carMapper.selectList(new QueryWrapper<Car>().eq("car_type_id", goodsTypeId).eq("deleted", DeletedEnum.FALSE));
        if (Optional.ofNullable(cars).isPresent()) {
            return -1;
        }else {
            removeById(goodsTypeId);
            return 1;
        }
    }

    @Override
    public GoodsType checkQueryById(String id) {
        GoodsType goodsType = goodsTypeMapper.selectById(id);
        Optional.ofNullable(goodsTypeMapper.selectList(new QueryWrapper<GoodsType>().eq("goods_type_parent_id",id))).ifPresent(goodsType::setGoodsTypeList);
        return goodsType;
    }

    @Override
    public List<GoodsType> quertList() {
        QueryWrapper<GoodsType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_type_parent_id","");
        return goodsTypeMapper.selectList(queryWrapper);
    }
}
